package com.unytech.wisdomcommunity.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.unytech.wisdomcommunity.R;
import com.viewpagerindicator.CirclePageIndicator;

/**
 * 向导界面
 */
public class GuideActivity extends AppCompatActivity {

    @ViewInject(R.id.guide_vp)
    ViewPager mViewPager;

    @ViewInject(R.id.guide_iv)
    ImageView mGuideIv;

    @ViewInject(R.id.guide_cpi)
    CirclePageIndicator mCirclePageIndicator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        ViewUtils.inject(this);

        mViewPager.setAdapter(mPageAdapter);
        mViewPager.addOnPageChangeListener(mPageListener);

        mCirclePageIndicator.setViewPager(mViewPager);
        // 设置圆圈颜色
        mCirclePageIndicator.setFillColor(Color.RED);//选中状态是红色
        mCirclePageIndicator.setPageColor(Color.WHITE);//未选中是白色
        mCirclePageIndicator.setStrokeColor(Color.BLACK);//圆形指示器的边框颜色
        mCirclePageIndicator.setStrokeWidth(0.5f);//圆形指示器的边框宽度
        // 设置半径大小 ，单位是像素
        int pxFor4Dp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources().getDisplayMetrics());
        mCirclePageIndicator.setRadius(pxFor4Dp);

        // 初始状态有问题：按钮可见
        //mButton.setVisibility(View.GONE);
        mPageListener.onPageSelected(0);

        mGuideIv.setOnClickListener(BtnOnclickListener);
    }

    private View.OnClickListener BtnOnclickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(GuideActivity.this, Main2Activity.class));
            finish();
        }
    };

    private int[] mPicRes = new int[]{R.drawable.guide_1, R.drawable.guide_2, R.drawable.guide_3};

    private PagerAdapter mPageAdapter = new PagerAdapter() {
        @Override
        public int getCount() {
            return mPicRes.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        // 预加载页面用到的方法
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView = new ImageView(container.getContext());
            imageView.setImageResource(mPicRes[position]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            container.addView(imageView);
            return imageView;
        }

        // 销毁页面用到的方法
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    };

    private ViewPager.OnPageChangeListener mPageListener = new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            if (position == mPageAdapter.getCount() - 1) {
                mGuideIv.setVisibility(View.VISIBLE);
            } else {
                mGuideIv.setVisibility(View.GONE);
            }
        }
    };
}